package com.ghh.dao;

import com.ghh.pojo.Member;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface MemberDao {
    //添加会员信息并返回会员id
    @Insert("insert into t_member values (#{id},#{fileNumber},#{name},#{sex},#{idCard},#{phoneNumber},#{regTime},#{password},#{email},#{birthday},#{remark})")
    @SelectKey(statement = "select last_insert_id()", before = false, keyProperty = "id", resultType = Integer.class, keyColumn = "id")
    void add(Member member);

    //根据手机号查询会员信息
    @Select("select * from t_member where phoneNumber=#{telephone}")
    Member findByTelephone(String telephone);

    //根据日期查询会员数
    @Select("select count(*) from t_member where regTime<#{date}")
    Integer getMemberReportByMonth(String date);

    @Select("SELECT date_format(t.regTime,'%Y-%m-%d') months, (select sum(temp.count) from (SELECT regTime, count(1) count FROM t_member GROUP BY regTime) temp where temp.regTime<=t.regTime) memberCount FROM (SELECT regTime, count(1) count FROM t_member GROUP BY regTime) t")
    List<Map<String, Object>> getDayAndMemner();

    @Select("select count(id) from t_member where regTime=#{reportDate}")
    Integer findMemberCountByDate(String reportDate);

    @Select("select count(*) from t_member")
    Integer findMemberTotalCount();

    @Select("select count(id) from t_member where regTime>=#{date}")
    Integer findMemberCountAfterDate(String date);

    @Select("<script>select * from t_member <if test=\"value!=null and value!=''\">where phoneNumber like '%${value}%' or name like '%${value}%'</if></script>")
    Page<Member> findByCondition(@Param("value") String queryString);

    @Delete("delete from t_member where id = #{id}")
    void deleteMember(int id);

    @Update("update t_member set fileNumber=#{fileNumber},name=#{name},sex=#{sex},idCard=#{idCard},phoneNumber=#{phoneNumber},type=#{type},regTime=#{regTime},password=#{password},email=#{email},birthday=#{birthday} where id = #{id}")
    void updateMember(Member member);

    @Select("select * from t_member where id=#{id}")
    Member findById(int id);

    @Select("select * from t_member")
    List<Member> findAll();
}
